package com.mhkj.workflow.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mhkj.workflow.entity.WorkFlowLogger;
import com.mhkj.workflow.service.WorkFlowLoggerService;
import com.mics.core.common.util.ftp.FtpUtil;
import com.mics.core.system.base.controller.BaseController;

/**
 * 项目
 * @author mics
 * @date 2018年5月15日
 * @version 1.0
 */
@Controller
@RequestMapping("/workFlowLogger")
public class WorkFlowLoggerController extends BaseController<WorkFlowLogger> {
	@Resource
	private WorkFlowLoggerService flowLoggerService;


	/**
	 * 根据流程id获取跳转URL
	 * @throws IOException 
	 */
	@RequestMapping("/getJumpUrlByProcInsId")
	@ResponseBody
	public String getJumpUrlByProcInsId(@RequestBody  String param) throws IOException{
		Properties properties = new Properties();
		properties.load(FtpUtil.class.getResourceAsStream("/workflow.properties"));
		String jumpUrl=properties.get("jumpUrl").toString();
		
		Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(currentTime);

		Map<String,Object> map=new HashMap<String, Object>();

	    map.put("respCode", "-1");
	    map.put("message", "服务器错误");
	    map.put("timeStamp", dateString);
	    map.put("data", "");
		JSONObject jsStr = JSONObject.parseObject(param);
		if(jsStr.get("procInsId")==null) {
		    map.put("respCode", "-2");
		    map.put("message", "参数不完整");
		    map.put("timeStamp", dateString);
		    map.put("data", "");
		    String jsonString = JSON.toJSONString(map);
			return jsonString;
		}
		String procInsId=jsStr.get("procInsId").toString();
		WorkFlowLogger list =flowLoggerService.getJumpUrlByProcInsId(procInsId);
		if(list==null) {
			 map.put("respCode", "2");
			    map.put("message", "返回数据为空");
			    map.put("timeStamp",dateString);
			    map.put("data", list);
		}else {
			Map<String,Object> urlMap=new HashMap<String, Object>();
			urlMap.put("jumpUrl", jumpUrl+list.getUrl());
		    map.put("respCode", "1");
		    map.put("message", "查询成功");
		    map.put("timeStamp", dateString);
		    map.put("data",urlMap);
		    
	    }
	    String jsonString = JSON.toJSONString(map);
		return jsonString;
	}
}
